package com.erp.activity

import android.Manifest
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.widget.RadioButton
import android.widget.RelativeLayout
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.erp.Interface.OnUserDataInterface
import com.erp.Myapp
import com.erp.R
import com.erp.bean.DrawerBean
import com.erp.contacts.Contacts
import com.erp.mclass.QuanXian
import com.erp.mclass.UpdataInfo
import com.erp.module.JiJianCaiJi.FragmentKJiJianCaiJi
import com.erp.module.ProductionExecuter.FragmengtProductionExecute
import com.erp.module.ProjectOrderProcess.FragmentProjectOrderProcess
import com.erp.module.YuliaoPrint.FragmengtYuliaoPrintLabel
import com.erp.module.anZhuangZHiNan.FragmentKAnZhuangZhiNan
import com.erp.module.article.FragmentArticle
import com.erp.module.caiLiaoZhiLiang.FragmentKCaiLiaoZhiLiang
import com.erp.module.crm.FragmentCrm
import com.erp.module.dingDan.FragmentDingDan
import com.erp.module.duizhangPhoto.FragmentDuizhangPhoto
import com.erp.module.faHuoDanJu.FragmentKWuLiuFaHuo
import com.erp.module.gongGao.FragmentGongGao
import com.erp.module.jiJian.FragmentKJiJian
import com.erp.module.jiaFang.FragmengtJiaFang
import com.erp.module.jiehao.FragmentKJieHao
import com.erp.module.jiehao.FragmentKZhiXiang
import com.erp.module.jingXiHua.FragmentJingXiHua
import com.erp.module.kaoShi.FragmentKKaoShi
import com.erp.module.oem.FragmentOem
import com.erp.module.packPiece.FragmentPackPiece
import com.erp.module.pieceworkGrouping.FragmentPieceworkGrouping
import com.erp.module.sheJiZhiNan.FragmentSheJiZhiNan
import com.erp.module.xiSuZhiXiang.FragmentXiSuZhiXiang
import com.erp.module.xianChangPaiZhao.FragmentKXianchangPaizhao
import com.erp.module.xieyi.XieyiActivity
import com.erp.module.yiJianXiang.FragmentYiJianXIang
import com.erp.module.yuliaofreight.FragmentYuliaoGiFreight
import com.erp.module.zhiLiang.FragmentZhiLiang
import com.erp.module.zhichengJianyan.FragmentProcessInspection
import com.erp.module.zhuanMaiDianTu.FragmentZhuanMaiDianTu
import com.erp.myAdapter.ClientPagerAdapter
import com.erp.notifiactionDb.PersonDB
import com.erp.permission.PermissionListener
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.util.PermissionUtils
import com.erp.view.MyAlerDialog
import com.taobao.sophix.SophixManager
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2016/11/28/028.
 * 作者：刘洋
 * activit内嵌fragmengt，用于显示数据
 */
class MainKActivity : BaseActivity(), View.OnClickListener {
    private val TAG = "MainActivity.class"

    lateinit var listfragment: MutableList<Fragment>
    lateinit var listrb: MutableList<RadioButton>
    lateinit var fragmentGongGao: FragmentGongGao
    lateinit var fragmentCrm: FragmentCrm
    lateinit var fragmentDingDan: FragmentDingDan
    lateinit var fragmentjijian: FragmentKJiJian
    lateinit var fragmentJiJianCaiJi: FragmentKJiJianCaiJi
    //    lateinit var fragmentFahuo: FragmentFahuo
    lateinit var fragmentKWuLiuFaHuo: FragmentKWuLiuFaHuo
    lateinit var fragmentKJieHao: FragmentKJieHao
    lateinit var fragmentKZhiXiang: FragmentKZhiXiang
    lateinit var fragmentHongLvDengZhiXiang: FragmentXiSuZhiXiang
    lateinit var fragmentKAnZhuangZhiNan: FragmentKAnZhuangZhiNan
    lateinit var fragmentKXianchangPaizhao: FragmentKXianchangPaizhao
    lateinit var fragmentZhuanMaiDianTu: FragmentZhuanMaiDianTu
    lateinit var fragmentSheJiZhiNan: FragmentSheJiZhiNan
    lateinit var fragmentZhiLiang: FragmentZhiLiang
    lateinit var fragmentTouSu: FragmentYiJianXIang
    lateinit var fragmentKaoShi: FragmentKKaoShi
    lateinit var fragmentKCaiLiaoZhiLiang: FragmentKCaiLiaoZhiLiang
    lateinit var fragmengtJiaFang: FragmengtJiaFang
    lateinit var fragmentPackPiece: FragmentPackPiece
    lateinit var fragmentArticle: FragmentArticle
    lateinit var fragmentJingXiHua: FragmentJingXiHua
    lateinit var fragmentprocessInspection: FragmentProcessInspection
    lateinit var fragmentProjectOrderProcess: FragmentProjectOrderProcess
    lateinit var fragmengtProductionExecute: FragmengtProductionExecute
    lateinit var fragmentDuizhangPhoto: FragmentDuizhangPhoto
    lateinit var fragmentOem: FragmentOem
    lateinit var fragmengtYuliaoPrintLabel: FragmengtYuliaoPrintLabel
    lateinit var fragmentYuliaoGiFreight: FragmentYuliaoGiFreight
    lateinit var fragmentPieceworkGrouping: FragmentPieceworkGrouping
    //    lateinit var fragmentTuXiang : FragmentTuXiang
    var isdrawerLayout = false
    var rightcode = 0

    /**
     * 拥有权限的集合
     */
    lateinit var listis: MutableList<String>
    /**
     * 广播接受着，用于在退出时关闭activity
     */
    private val mBoradcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (intent.action == Contacts.BORADCAST_ACTION_EXIT) {//关闭app的广播
                val personDB = PersonDB.getIntance()
                personDB.clear()
                finish()
            } else if (intent.action == Contacts.BORADCAST_ACTION_NOTI) {//收到通知的广播
                isnotification()
            } else if (intent.action == Contacts.BORADCAST_ACTION_UPDATE) {//更新的广播
                UpdataInfo(this@MainKActivity, false, sp).show()
            } else if (intent.action == Contacts.BORADCAST_ACTION_SOPHIX) {
                Myutil.MyTost("SophixStubApplication")
                Log.i("SophixStubApplication", "下载了新补丁需要重启应用.")
                val activity = Myapp.list[Myapp.list.size - 1]
                val myAlerDialog = MyAlerDialog(activity)
                myAlerDialog.start()
                myAlerDialog.setdata("下载了新补丁需要重启应用，点击确定立即重启！")
                myAlerDialog.setleft("确定") { SophixManager.getInstance().killProcessSafely() }
                myAlerDialog.setright("取消") { myAlerDialog.getdialog().dismiss() }
                myAlerDialog.show()
            }
        }
    }

    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_main
    }

    override fun initCreate() {
        // 在当前的activity中注册广播
        val filter = IntentFilter()
        filter.addAction(Contacts.BORADCAST_ACTION_EXIT)//为BroadcastReceiver指定一个action，即要监听的消息名字
        filter.addAction(Contacts.BORADCAST_ACTION_NOTI)//为BroadcastReceiver指定一个action，即要监听的消息名字
        filter.addAction(Contacts.BORADCAST_ACTION_UPDATE)//为BroadcastReceiver指定一个action，即要监听的消息名字
        filter.addAction(Contacts.BORADCAST_ACTION_SOPHIX)//为BroadcastReceiver指定一个action，即要监听的消息名字
        registerReceiver(mBoradcastReceiver, filter) //动态注册监听  静态的话 在AndroidManifest.xml中定义
        listis = ArrayList()
        isnotification()
        drawerLayout()
        initView()
        initonclick()
        UpdataInfo(this, false, sp).show()
        tvTitle.isFocusable = true
        tvTitle.isFocusableInTouchMode = true
        tvTitle.requestFocus()
        tvTitle.requestFocusFromTouch()
        PermissionUtils.isupdate(this@MainKActivity, object : PermissionListener {
            override fun onSucceed(requestCode: Int, grantPermissions: List<String>) = Unit

            override fun onFailed(requestCode: Int, deniedPermissions: List<String>) = Unit
        })
        SophixManager.getInstance().queryAndLoadNewPatch()

        //if android version > 6.0, you will need some premission
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
//                    || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//                //请求权限
//                ActivityCompat.requestPermissions(this, arrayOf<String>(Manifest.permission.ACCESS_FINE_LOCATION,
//                        Manifest.permission.ACCESS_COARSE_LOCATION), 1)
//            } else {
//            }
//        } else {
//        }
    }

    private fun isnotification() {
        val personDB: PersonDB
        personDB = PersonDB.getIntance()
        val notificationBeens = personDB.findfase(false)
        if (notificationBeens != null) {
            if (notificationBeens.size != 0) {
                ivTongzhi.visibility = View.VISIBLE
                IvNotification.setImageResource(R.mipmap.erp_message1)
            } else {
                ivTongzhi.visibility = View.GONE
                IvNotification.setImageResource(R.mipmap.erp_message)
            }
        }
    }

    private fun drawerLayout() {
        var mDrawerToggle = object : ActionBarDrawerToggle(this, DlLeft, iv_header_left, R.string.open, R.string.close) {
            override fun onDrawerOpened(drawerView: View) {
                isdrawerLayout = true
                super.onDrawerOpened(drawerView)

            }

            override fun onDrawerClosed(drawerView: View) {
                isdrawerLayout = false
                super.onDrawerClosed(drawerView)
            }
        }
        mDrawerToggle.syncState()
        DlLeft.addDrawerListener(mDrawerToggle)
        val user = sp.getString("user", "")
        Log.d("TAAG", "user=" + user)
        val drawerBean = DrawerBean()
        val quanXian = QuanXian()
        try {
            val jo1 = JSONObject(user)
            uid = jo1.getString("uid")
            name = jo1.getString("name")
            editor.putString("uid", uid)
            drawerBean.mname = jo1.getString("name")
            drawerBean.youxiang = jo1.getString("mail")
            drawerBean.zhuceshijian = jo1.getString("created")
            drawerBean.denglushijian = jo1.getString("login")
            drawerBean.shangcidenglu = jo1.getString("access")
            val jo2 = jo1.getJSONObject("kerp")
            drawerBean.bieming = jo2.getString("alias_name")
            drawerBean.daoqishijian = jo2.getString("expire_time")
            ZhuanMaiDianid = JsonUtil.getvolue(jo2, "exclusive_shop")
            val jo3 = jo1.getJSONObject("permissions")
            quanXian.geuQuanXian(jo3)
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        quanXian.baocun(editor)
        Myutil.settv(drawerBean.mname, TvMyName)
        Myutil.settv(drawerBean.youxiang, TvYouxiang)
        Myutil.settv(Myutil.getdata(drawerBean.daoqishijian), TvDaoqishijian)
        Myutil.settv(drawerBean.bieming, TvBieming)
        Myutil.settv(Myutil.getdata(drawerBean.shangcidenglu), TvShangcidenglu)
        Myutil.settv(Myutil.getdata(drawerBean.zhuceshijian), TvZhuceshijian)
        Myutil.settv(Myutil.getdata(drawerBean.denglushijian), TvDenglushijian)
    }

    /**
     * pager滑动监听
     */
    private fun initonclick() {
        PagerMain.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

            override fun onPageSelected(position: Int) {
                tv_header_right.visibility = View.GONE
                iv_header_right.visibility = View.VISIBLE
                tvTitle.text = listis[position]
                listrb[position].isChecked = true
                rightcode = 0
            }

            override fun onPageScrollStateChanged(state: Int) = Unit
        })
        IvDownload.setOnClickListener(this)
        IvNotification.setOnClickListener(this)
        LlGengxing.setOnClickListener(this)
        RadionuttomZero.setOnClickListener(this)
        RadionuttomOne.setOnClickListener(this)
        RadionuttomJijianCaiji.setOnClickListener(this)
        RadionuttomTwo.setOnClickListener(this)
//        RadionuttomThree.setOnClickListener(this)
        RadionuttomFaHuo.setOnClickListener(this)
        RadionuttomFour.setOnClickListener(this)
        RadionuttomFive.setOnClickListener(this)
        RadionuttomSix.setOnClickListener(this)
        RadionuttomFenBanZhiXiang.setOnClickListener(this)
        RadionuttomSeven.setOnClickListener(this)
        RadionuttomEight.setOnClickListener(this)
        RadionuttomNine.setOnClickListener(this)
        RadionuttomTen.setOnClickListener(this)
        RadionuttomTousu.setOnClickListener(this)
        RadionuttomTwelve.setOnClickListener(this)
        RadionuttomCailiaoZhiliang.setOnClickListener(this)
        RadionuttomKaoshi.setOnClickListener(this)
        RadionuttomJiaFang.setOnClickListener(this)
        RadionuttomPackPiece.setOnClickListener(this)
        RadionuttomArticle.setOnClickListener(this)
        RadionuttomJingXiHua.setOnClickListener(this)
        Radionuttominspection.setOnClickListener(this)
        RadioProjectOrderProcess.setOnClickListener(this)
        RadioProductionExecute.setOnClickListener(this)
        RadioStatementAccountPhoto.setOnClickListener(this)
        RadioYuliaoPrintLabel.setOnClickListener(this)
        RadioOem.setOnClickListener(this)
        RadioYuliaoFreight.setOnClickListener(this)
        RadioPieceworkGrouping.setOnClickListener(this)
//        RadionuttomTuXiang.setOnClickListener(this)
        tv_header_right.setOnClickListener(this)
        iv_header_right.setOnClickListener(this)
        LlQuit.setOnClickListener(this)
        Tv_Fuwu.setOnClickListener(this)
        Tv_Yingsi.setOnClickListener(this)
    }

    /**
     * 初始化数据和加载布局
     */
    private fun initView() {
        listfragment = ArrayList()
        listrb = ArrayList()
        if (QuanXian.isgonggao) {
            listis.add("公 告")
            listrb.add(RadionuttomZero)
            fragmentGongGao = FragmentGongGao()
            listfragment.add(fragmentGongGao)
        }
        if (QuanXian.iscrm) {
            listis.add("客户管理")
            listrb.add(RadionuttomTwo)
            fragmentCrm = FragmentCrm()
            listfragment.add(fragmentCrm)
        }
        if (QuanXian.isscdd) {
            listis.add("订单进度")
            listrb.add(RadionuttomFour)
            fragmentDingDan = FragmentDingDan()
            listfragment.add(fragmentDingDan)
        }
        if (QuanXian.isdeliver) {
            listis.add("单据")
            listrb.add(RadionuttomFaHuo)
            fragmentKWuLiuFaHuo = FragmentKWuLiuFaHuo()
            listfragment.add(fragmentKWuLiuFaHuo)
        }
        if (QuanXian.isjijian) {
            listis.add("计件")
            listrb.add(RadionuttomOne)
            fragmentjijian = FragmentKJiJian()
            listfragment.add(fragmentjijian)
        }
        if (QuanXian.isJiJianCaiJi) {
            listis.add("计件采集")
            listrb.add(RadionuttomJijianCaiji)
            fragmentJiJianCaiJi = FragmentKJiJianCaiJi()
            listfragment.add(fragmentJiJianCaiJi)
        }
        if (QuanXian.ischakanjiehao) {
            listis.add("节号分配")
            listrb.add(RadionuttomFive)
            fragmentKJieHao = FragmentKJieHao()
            listfragment.add(fragmentKJieHao)
        }
        if (QuanXian.isChaKanZhiXiangFenPei) {
            listis.add("纸箱分配")
            listrb.add(RadionuttomSix)
            fragmentKZhiXiang = FragmentKZhiXiang()
            listfragment.add(fragmentKZhiXiang)
        }
        if (QuanXian.isViewBoardCartonDistribution) {
            listis.add("吸塑纸箱")
            listrb.add(RadionuttomFenBanZhiXiang)
            fragmentHongLvDengZhiXiang = FragmentXiSuZhiXiang()
            listfragment.add(fragmentHongLvDengZhiXiang)
        }
        if (QuanXian.isXingKaiZhuanMaiDian) {
            listis.add("专卖店列表")
            listrb.add(RadionuttomNine)
            fragmentZhuanMaiDianTu = FragmentZhuanMaiDianTu()
            listfragment.add(fragmentZhuanMaiDianTu)
        }
        if (QuanXian.isChaKanWuLiaoZhiLiang) {
            listis.add("板材质量")
            listrb.add(RadionuttomTen)
            fragmentZhiLiang = FragmentZhiLiang()
            listfragment.add(fragmentZhiLiang)
        }
        if (QuanXian.isSheJiZhiNan) {
            listis.add("设计指南")
            listrb.add(RadionuttomTwelve)
            fragmentSheJiZhiNan = FragmentSheJiZhiNan()
            listfragment.add(fragmentSheJiZhiNan)
        }
        if (QuanXian.isShuangChuanAnZhuang) {
            listis.add("安装管理")
            listrb.add(RadionuttomEight)
            fragmentKXianchangPaizhao = FragmentKXianchangPaizhao()
            listfragment.add(fragmentKXianchangPaizhao)
        }
        if (QuanXian.isAnZhuangZhiNan) {
            listis.add("安装指南")
            listrb.add(RadionuttomSeven)
            fragmentKAnZhuangZhiNan = FragmentKAnZhuangZhiNan()
            listfragment.add(fragmentKAnZhuangZhiNan)
        }
        if (QuanXian.isZhuanMaiDianKaoShi || QuanXian.isShengChanZhongXingKaoShi || QuanXian.isYingXiaoZhongXingKaoShi) {
            listis.add("考试")
            listrb.add(RadionuttomKaoshi)
            fragmentKaoShi = FragmentKKaoShi()
            listfragment.add(fragmentKaoShi)
        }
        if (QuanXian.isChaKanCaiLiaoZhiLiang) {
            listis.add("来料质量")
            listrb.add(RadionuttomCailiaoZhiliang)
            fragmentKCaiLiaoZhiLiang = FragmentKCaiLiaoZhiLiang()
            listfragment.add(fragmentKCaiLiaoZhiLiang)
        }
        if (QuanXian.isChakanJiaFang) {
            listis.add("家纺")
            listrb.add(RadionuttomJiaFang)
            fragmengtJiaFang = FragmengtJiaFang()
            listfragment.add(fragmengtJiaFang)
        }
        if (QuanXian.isViewPackPiece) {
            listis.add("包装计件")
            listrb.add(RadionuttomPackPiece)
            fragmentPackPiece = FragmentPackPiece()
            listfragment.add(fragmentPackPiece)
        }
        if (QuanXian.isViewArticle) {
            listis.add("卡诺亚资料")
            listrb.add(RadionuttomArticle)
            fragmentArticle = FragmentArticle()
            listfragment.add(fragmentArticle)
        }
        if (QuanXian.isViewElaborateManagement) {
            listis.add("精细化")
            listrb.add(RadionuttomJingXiHua)
            fragmentJingXiHua = FragmentJingXiHua()
            listfragment.add(fragmentJingXiHua)
        }
        if (QuanXian.isChaKanTouSuJianYi) {
            listis.add("意见箱")
            listrb.add(RadionuttomTousu)
            fragmentTouSu = FragmentYiJianXIang()
            listfragment.add(fragmentTouSu)
        }
        if (QuanXian.isViewProcessInspection) {
            listis.add("制程检验")
            listrb.add(Radionuttominspection)
            fragmentprocessInspection = FragmentProcessInspection()
            listfragment.add(fragmentprocessInspection)
        }
        if(QuanXian.isEditProjectOrderProcess){
            listis.add("工程单进度")
            listrb.add(RadioProjectOrderProcess)
            fragmentProjectOrderProcess =  FragmentProjectOrderProcess()
            listfragment.add(fragmentProjectOrderProcess)
        }
        if(QuanXian.isProductionDispatchExecute || QuanXian.isProductionDispatchAccomplish){
            listis.add("上料执行")
            listrb.add(RadioProductionExecute)
            fragmengtProductionExecute =  FragmengtProductionExecute()
            listfragment.add(fragmengtProductionExecute)
        }
        if(QuanXian.isViewStatementAccountPhoto){
            listis.add("对账上传")
            listrb.add(RadioStatementAccountPhoto)
            fragmentDuizhangPhoto =  FragmentDuizhangPhoto()
            listfragment.add(fragmentDuizhangPhoto)
        }
        if(QuanXian.isViewOemOrder){
            listis.add("采购管理")
            listrb.add(RadioOem)
            fragmentOem =  FragmentOem()
            listfragment.add(fragmentOem)
        }
        if(QuanXian.isPrintYuliaoLabel){
            listis.add("余料打印")
            listrb.add(RadioYuliaoPrintLabel)
            fragmengtYuliaoPrintLabel =  FragmengtYuliaoPrintLabel()
            listfragment.add(fragmengtYuliaoPrintLabel)
        }
        if(QuanXian.isViewYuliaoFreightSpaceGi){
            listis.add("余料仓")
            listrb.add(RadioYuliaoFreight)
            fragmentYuliaoGiFreight =  FragmentYuliaoGiFreight()
            listfragment.add(fragmentYuliaoGiFreight)
        }
        if(QuanXian.isGatherPieceworkColl){
            listis.add("计件采集")
            listrb.add(RadioPieceworkGrouping)
            fragmentPieceworkGrouping =  FragmentPieceworkGrouping()
            listfragment.add(fragmentPieceworkGrouping)
        }


        val width = sp.getInt("width", 0)
        val params = RgDibu.layoutParams as RelativeLayout.LayoutParams
        if (listis.size > 5) {
            params.width = width / 5 * listis.size
        } else {
            params.width = width
        }
        RgDibu.layoutParams = params
        for (i in listis.indices) {
            listrb[i].visibility = View.VISIBLE
            listrb[i].text = listis[i]
        }
        if (listis.size > 0) {
            tvTitle.text = listis[0]
        }
        val vpadapter = ClientPagerAdapter(supportFragmentManager, listfragment)
        PagerMain.adapter = vpadapter
        PagerMain.offscreenPageLimit = listfragment.size
        RadionuttomZero.isChecked = true
    }

    /**
     * onClick方法，用于点击radiobutton是切换fragment
     *
     * @param view
     */
    override fun onClick(v: View) {

        when (v) {
            IvNotification -> {
                isnotification()
                val intent1 = Intent(this@MainKActivity, NotificationActivity::class.java)
                startActivityForResult(intent1, 105)
            }
            IvDownload -> {
                val intent1 = Intent(this@MainKActivity, LingShi1Activity::class.java)
                startActivity(intent1)
            }
            LlGengxing -> UpdataInfo(this@MainKActivity, true, sp).show()
            iv_header_right -> {
                val intent = Intent(this@MainKActivity, DialogActivity::class.java)
                //                startActivity(intent);
                startActivityForResult(intent, 101)
            }
            RadionuttomZero -> {
                RadionuttomZero.isChecked = true
                var isFirstLoad = fragmentGongGao.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomZero)
                rightqiehuan()
                if(!isFirstLoad){
                    fragmentGongGao.qingkong()
                }
            }
            RadionuttomOne -> {
                RadionuttomOne.isChecked = true
                var isFirstLoad = fragmentjijian.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomOne)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentjijian.qingkong()
                }
            }
            RadionuttomJijianCaiji -> {
                RadionuttomJijianCaiji.isChecked = true
                var isFirstLoad = fragmentJiJianCaiJi.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomJijianCaiji)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentJiJianCaiJi.qingkong()
                }
            }
            RadionuttomTwo -> {
                RadionuttomTwo.isChecked = true
                var isFirstLoad = fragmentCrm.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomTwo)
                rightqiehuan()
                if(!isFirstLoad){
                    fragmentCrm.qingkong()
                }
            }
//            RadionuttomThree -> {
//                RadionuttomThree.isChecked = true
//                PagerMain.currentItem = listrb.indexOf(RadionuttomThree)
//                rightqiehuan()
//            }
            RadionuttomFaHuo -> {
                RadionuttomFaHuo.isChecked = true
                PagerMain.currentItem = listrb.indexOf(RadionuttomFaHuo)
                rightqiehuan()
            }
            RadionuttomFour -> {
                RadionuttomFour.isChecked = true
                var isFirstLoad = fragmentDingDan.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomFour)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentDingDan.qingkong()
                }
            }
            RadionuttomFive -> {
                RadionuttomFive.isChecked = true
                var isFirstLoad = fragmentKJieHao.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomFive)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentKJieHao.qingkong()
                }
            }
            RadionuttomSix -> {
                RadionuttomSix.isChecked = true
                var isFirstLoad = fragmentKZhiXiang.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomSix)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentKZhiXiang.qingkong()
                }
            }
            RadionuttomFenBanZhiXiang -> {
                RadionuttomFenBanZhiXiang.isChecked = true
                var isFirstLoad = fragmentHongLvDengZhiXiang.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomFenBanZhiXiang)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentHongLvDengZhiXiang.qingkong()
                }
            }
            RadionuttomSeven -> {
                RadionuttomSeven.isChecked = true
                var isFirstLoad = fragmentKAnZhuangZhiNan.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomSeven)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentKAnZhuangZhiNan.qingkong()
                }
            }
            RadionuttomEight -> {
                RadionuttomEight.isChecked = true
                var isFirstLoad = fragmentKXianchangPaizhao.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomEight)
                rightqiehuan()
                fragmentKXianchangPaizhao.qingkong()
            }
            RadionuttomNine -> {
                RadionuttomNine.isChecked = true
                var isFirstLoad = fragmentZhuanMaiDianTu.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomNine)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentZhuanMaiDianTu.qingkong()
                }
            }
            RadionuttomPackPiece -> {
                RadionuttomPackPiece.isChecked = true
                var isFirstLoad = fragmentPackPiece.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomPackPiece)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentPackPiece.qingkong()
                }
            }
            RadionuttomJingXiHua -> {
                RadionuttomJingXiHua.isChecked = true
                var isFirstLoad = fragmentJingXiHua.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomJingXiHua)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentJingXiHua.qingkong()
                }
            }
            Radionuttominspection -> {
                Radionuttominspection.isChecked = true
                var isFirstLoad = fragmentprocessInspection.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(Radionuttominspection)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentprocessInspection.qingkong()
                }
            }
            RadioProjectOrderProcess -> {
                RadioProjectOrderProcess.isChecked = true
                PagerMain.currentItem = listrb.indexOf(RadioProjectOrderProcess)
                rightqiehuan()
            }
//            RadionuttomTuXiang -> {
//                RadionuttomTuXiang.isChecked = true
//                PagerMain.currentItem = listrb.indexOf(RadionuttomTuXiang)
//                rightqiehuan()
//            }
            RadionuttomTen -> {
                RadionuttomTen.isChecked = true
                var isFirstLoad = fragmentZhiLiang.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomTen)
                rightqiehuan()
//                if (QuanXian.isXiuGaiWuLiaoZhiLiang) {
//                    tv_header_right.visibility = View.VISIBLE
//                    iv_header_right.visibility = View.GONE
//                    tv_header_right.text = "新建"
//                    rightcode = 1
//                }
                if(!isFirstLoad){

                    fragmentZhiLiang.qingkong()
                }
            }
            RadionuttomTousu -> {
                RadionuttomTousu.isChecked = true
                var isFirstLoad = fragmentTouSu.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomTousu)
                rightqiehuan()
//                if (QuanXian.isXiuGaiTouSuJianYi) {
//                    tv_header_right.visibility = View.VISIBLE
//                    iv_header_right.visibility = View.GONE
//                    tv_header_right.text = "新建"
//                    rightcode = 2
//                }
                if(!isFirstLoad){

                    fragmentTouSu.qingkong()
                }
            }
            RadionuttomTwelve -> {
                RadionuttomTwelve.isChecked = true
                var isFirstLoad = fragmentSheJiZhiNan.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomTwelve)
                rightqiehuan()
                if(!isFirstLoad){

                    fragmentSheJiZhiNan.qingkong()
                }
            }
            RadionuttomCailiaoZhiliang -> {
                RadionuttomCailiaoZhiliang.isChecked = true
                var isFirstLoad = fragmentKCaiLiaoZhiLiang.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomCailiaoZhiliang)
                rightqiehuan()
//                if (QuanXian.isXiuGaiTouSuJianYi) {
//                    tv_header_right.visibility = View.VISIBLE
//                    iv_header_right.visibility = View.GONE
//                    tv_header_right.text = "新建"
//                    rightcode = 3
//                }
                if(!isFirstLoad){

                    fragmentKCaiLiaoZhiLiang.qingkong()
                }
            }
            RadionuttomKaoshi -> {
                RadionuttomKaoshi.isChecked = true
                PagerMain.currentItem = listrb.indexOf(RadionuttomKaoshi)
                rightqiehuan()
            }
            RadionuttomJiaFang -> {
                RadionuttomJiaFang.isChecked = true
                var isFirstLoad = fragmengtJiaFang.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomJiaFang)
                rightqiehuan()
//                if (QuanXian.isEditJiaFang) {
//                    tv_header_right.visibility = View.VISIBLE
//                    iv_header_right.visibility = View.GONE
//                    tv_header_right.text = "新建"
//                    rightcode = 5
//                }
                if(!isFirstLoad){

                    fragmengtJiaFang.qingkong()
                }
            }
            RadionuttomArticle -> {
                RadionuttomArticle.isChecked = true
                var isFirstLoad = fragmentArticle.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadionuttomArticle)
                rightqiehuan()
                if(!isFirstLoad){
                    fragmentArticle.qingkong()
                }
            }
            RadioProductionExecute -> {
                RadioProductionExecute.isChecked = true
                var isFirstLoad = fragmengtProductionExecute.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadioProductionExecute)
                rightqiehuan()
                if(!isFirstLoad){
                    fragmengtProductionExecute.qingkong()
                }
            }
            RadioStatementAccountPhoto -> {
                RadioStatementAccountPhoto.isChecked = true
                var isFirstLoad = fragmentDuizhangPhoto.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadioStatementAccountPhoto)
                rightqiehuan()
                if(!isFirstLoad){
                    fragmentDuizhangPhoto.qingkong()
                }
            }
            RadioOem -> {
                RadioOem.isChecked = true
                var isFirstLoad = fragmentOem.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadioOem)
                rightqiehuan()
                if(!isFirstLoad){
                    fragmentOem.qingkong()
                }
            }
            RadioYuliaoPrintLabel -> {
                RadioOem.isChecked = true
                var isFirstLoad = fragmengtYuliaoPrintLabel.isFirstLoad
                PagerMain.currentItem = listrb.indexOf(RadioYuliaoPrintLabel)
                rightqiehuan()
                if(!isFirstLoad){
                    fragmengtYuliaoPrintLabel.qingkong()
                }
            }
            RadioYuliaoFreight -> {
                RadioOem.isChecked = true
                PagerMain.currentItem = listrb.indexOf(RadioYuliaoFreight)
                rightqiehuan()
            }
            RadioPieceworkGrouping-> {
                RadioOem.isChecked = true
                PagerMain.currentItem = listrb.indexOf(RadioPieceworkGrouping)
                rightqiehuan()
            }
            tv_header_right -> when (rightcode) {
                1 -> fragmentZhiLiang.xingjian()
                2 -> fragmentTouSu.xingjian()
                3 -> fragmentKCaiLiaoZhiLiang.xingjian()
                4 -> fragmentCrm.xingjian()
                5 -> fragmengtJiaFang.addjiafang()
            }
            LlQuit -> {
                val myAlerDialog = MyAlerDialog(this)
                myAlerDialog.start()
                myAlerDialog.setdata("确定退出登录吗？")
                myAlerDialog.setleft("确定") {
                    myAlerDialog.dismiss()
                    var updatedialog = Myutil.showupdatedialog(this@MainKActivity, "正在退出登录", true)
                    Httputil.setLogout(this@MainKActivity, object : OnUserDataInterface {
                        override fun onUserData(data: String) {
                            myAlerDialog.dismiss()
                            if (data.indexOf("true") != -1) {
                                Myutil.MyTost("退出登录状态成功")
                                editor.putBoolean("islogin", false)
                                editor.commit()
                                finish()
                            }
                        }

                        override fun onNot(throwable: String) {
                            myAlerDialog.dismiss()
                            if (updatedialog != null) {
                                updatedialog.dismiss()
                            }
                            Myutil.MyTost("网络访问失败")
                        }
                    });
                }
                myAlerDialog.setright("取消") { myAlerDialog.dismiss() }
                myAlerDialog.show()

            }
            Tv_Fuwu -> {
                val intent = Intent(this, XieyiActivity::class.java)
                intent.putExtra("code", 1)
                startActivity(intent)
            }
            Tv_Yingsi -> {
                val intent = Intent(this, XieyiActivity::class.java)
                intent.putExtra("code", 2)
                startActivity(intent)
            }
        }
    }

    private fun rightqiehuan() {
        tv_header_right.visibility = View.GONE
        iv_header_right.visibility = View.VISIBLE
        rightcode = 0
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        when (requestCode) {
            101 -> {
                val select = data.getIntExtra("select", 0)
                if (select == 21) {
                    val s = data.getStringExtra("s")
                    fragmentKXianchangPaizhao.isTiao = true
                    PagerMain.currentItem = listfragment.indexOf(fragmentKXianchangPaizhao)
                    fragmentKXianchangPaizhao.update(s)
                } else {
                    val s = data.getStringExtra("all")
                    fragmentDingDan.isTiao = true
                    fragmentDingDan.setSelect(select)
                    PagerMain.currentItem = listfragment.indexOf(fragmentDingDan)
                    fragmentDingDan.setUpdate(s)
                    fragmentDingDan.update()
                }
            }
            105 -> {
                isnotification()
            }

        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        Log.d("onKeyDown", "onKeyDown+keyCode=" + keyCode);
        Log.d("onKeyDown", "onKeyDown+keyCode=" + keyCode);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.d("onKeyDown", "keyCode=" + keyCode);
            Log.d("onKeyDown", "keyCode=" + keyCode);
            if (sp.getBoolean("Sophix", false)) {
//                Log.d("SophixManager", "去安装补丁")
                SophixManager.getInstance().killProcessSafely();
//                return false
            } else {
                if (isdrawerLayout) {
                    DlLeft.closeDrawers()
                } else {
                    finish()
//                    IXJWuLiaoZhiLiangKp.gonghaos = ""
//                    IXJWuLiaoZhiLiangKp.zhiliangs = ""
//                    val intent = Intent(Intent.ACTION_MAIN)
//                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK// 注意
//                    intent.addCategory(Intent.CATEGORY_HOME)
//                    this.startActivity(intent)
                }
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    //    override fun onConfigurationChanged(newConfig: Configuration?) {
//        super.onConfigurationChanged(newConfig)
//    }
    companion object {
        /**
         * MainActivity是否启动
         */
        var uid = ""
        var name = ""
        /**专卖店账号 */
        var ZhuanMaiDianid = ""
        private val a = "MainActivity"
    }
}
